package com.swiftops.basic.login.controller;

import com.swiftops.basic.login.service.SysLoginService;
import com.swiftops.basic.login.vo.entity.SysLoginEntityVO;
import com.swiftops.common.annotation.IgnoreSecurity;
import com.swiftops.common.core.ApiResult;
import com.swiftops.common.model.TokenModel;
import com.swiftops.common.util.BindingResultUtil;
import com.swiftops.common.util.JasyptUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户信息
 *
 * @author yangyang
 * @date 2023/09/17
 **/
@Slf4j
@RestController
@RequestMapping("/api/v1/sys")
@Api(description = "登录")
public class SysLoginController {

    @Autowired
    private SysLoginService sysLoginService;

    @ApiOperation("登录,生成token，返回用户信息")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResponseEntity<ApiResult<Object>> login(@RequestBody @Validated SysLoginEntityVO entityVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BindingResultUtil.getErrorMessage(bindingResult);
        }
        try {
            TokenModel vo = this.sysLoginService.login(entityVO);
            return ApiResult.ok(vo);
        } catch (Exception e) {
            return ApiResult.error(e);
        }
    }

    @ApiOperation("退出登录")
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public ResponseEntity<ApiResult<Object>> logout() {
        try {
            this.sysLoginService.logout();
            return ApiResult.ok();
        } catch (Exception e) {
            return ApiResult.error(e);
        }
    }

//    @IgnoreSecurity
//    @ApiOperation("登录,生成token，返回用户信息")
//    @RequestMapping(value = "/decrypt", method = RequestMethod.POST)
//    public ResponseEntity<ApiResult<Object>> decrypt(@RequestBody @Validated SysLoginEntityVO entityVO, BindingResult bindingResult) {
//        if (bindingResult.hasErrors()) {
//            return BindingResultUtil.getErrorMessage(bindingResult);
//        }
//        String decrypt = JasyptUtils.decrypt(entityVO.getPassword());
//        return ApiResult.ok(decrypt);
//    }
}
